package com.doyens.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.reggie.commons.R;
import com.doyens.reggie.domain.Category;
import com.doyens.reggie.domain.Setmeal;
import com.doyens.reggie.dto.SetmealDto;
import com.doyens.reggie.service.CategoryService;
import com.doyens.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    // 修改
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }

    @GetMapping("list")
    @ApiOperation(value = "根据分类id查询接口")
    @Cacheable(value = "setmealCache",key = "#categoryId")
    public R<List<Setmeal>> list(Long categoryId){
        List<Setmeal> setmealList = setmealService.list(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, categoryId));
        return R.success(setmealList);
    }

    // 根据套餐的id查询套餐  做回显
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐接口")
    public R<SetmealDto> findSetmealById(@PathVariable long id){
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    // 起售和停售
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("status/{status}")
    @ApiOperation(value = "修改套餐接口")
    public R<String> updateStatus(@PathVariable int status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealService.update(setmeal,wrapper);
        return R.success("修改成功");
    }

    // 批量删除
    @CacheEvict(value = "setmealCache",allEntries = true)  //同步缓存
    @DeleteMapping
    @ApiOperation(value = "套餐删除接口")
    public R<String> delSetmeal(@RequestParam List<Long> ids){
        // 批量删除  1  2  3
        setmealService.delWithSetmealDish(ids);
        return R.success("删除成功");
    }

    // 分页查询套餐
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page> findPage(int page,int pageSize,@RequestParam(value = "name",required = false) String name){
        Page<Setmeal> params = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(name), Setmeal::getName,name);
        Page<Setmeal> pb = setmealService.page(params, wrapper);
        // pb{total:2  records:[{Setmeal},{Setmeal}]}
        // pbDto{total:2  records:[{SetmealDto},{SetmealDto}]}

        List<Setmeal> setmealList = pb.getRecords();
        // stream流
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);// categoryName没有值
            // 做查询
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        Page<SetmealDto> setmealDtoPb = new Page<>();
        setmealDtoPb.setTotal(pb.getTotal());
        setmealDtoPb.setRecords(setmealDtoList);
        return R.success(setmealDtoPb);
    }

    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
        log.info("添加套餐数据为：" + setmealDto);
        setmealService.saveSetmeal(setmealDto);
        return R.success("添加成功");
    }
}
